package ext.generic.util;

import java.util.LinkedList;
import java.util.List;

import wt.util.WTException;

import com.ptc.core.components.descriptor.ComponentDescriptor;
import com.ptc.core.components.descriptor.DataUtility;
import com.ptc.core.components.descriptor.DataUtilityHelper;
import com.ptc.core.components.descriptor.DescriptorHelper;
import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.dataUtilities.DefaultDataUtility;

public class MyATIDataUtility extends DefaultDataUtility {

    private static final List<String> myAttrs;

    static {
        List<String> temp = new LinkedList<String>();
        temp.add("MultiDemo");

        myAttrs = temp;
    }

    @Override
    public Object getDataValue(String component_id, Object datum, ModelContext mc) throws WTException {

        Object value;
        String last = component_id.substring(component_id.lastIndexOf("|") + 1, component_id.length());

        if (!myAttrs.contains(last)) {
            return super.getDataValue(component_id, datum, mc);
        }

        ComponentDescriptor descriptor = DescriptorHelper.getDescriptor(last);

        DataUtility mydataUtility = DataUtilityHelper.getDataUtility(descriptor); // try to find the custom datautility for the IBA
        if (mydataUtility == null) {
            descriptor.setId(last);
        }
        mydataUtility = DataUtilityHelper.getDataUtility(descriptor); // try to find the custom datautility for the IBA


        if (mydataUtility == null) {
            value = DataUtilityHelper.getDefaultDataValue(component_id, datum, mc);
        } else {
            value = mydataUtility.getDataValue(component_id, datum, mc);
        }

        return value;
    }
}